package com.edu.schoolforum.controller;

import com.edu.schoolforum.dto.*;
import com.edu.schoolforum.enums.ResultEnum;
import com.edu.schoolforum.mapper.UserMapper;
import com.edu.schoolforum.pojo.User;
import com.edu.schoolforum.utils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author tao
 * @date 2021/8/11 17:17
 */
@Controller
@RestController
@RequestMapping("/api")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @PostMapping("/updateUserPassword")
    public CommonResult updateUserPassword(@RequestBody UpdatePasswordRequestDto updatePasswordRequestDto){
          User user=userMapper.getUserByName(updatePasswordRequestDto.getName());
          if(user!=null&&user.getPassword().equals(updatePasswordRequestDto.getOldPassword())){
              User newUser=new User();
              newUser.setPassword(updatePasswordRequestDto.getNewPassword());
              newUser.setName(updatePasswordRequestDto.getName());
              int i=userMapper.updateUserPassword(newUser);
              return CommonResult.success();
          }
          return CommonResult.fail(ResultEnum.UPDATE_MESSAGE_FAIL);
    }

    @PostMapping("/updateUserAuthority")
    public CommonResult updateUserAuthority(@RequestBody UpdateUserAuthorityDto updateUserAuthorityDto){
        int i=userMapper.updateUserAuthority(updateUserAuthorityDto);
        if(i!=0){
            return CommonResult.success();
        }else{
            return CommonResult.fail(ResultEnum.UPDATE_MESSAGE_FAIL);
        }
    }

    @PostMapping("/updateUserStatus")
    public CommonResult updateUserStatus(@RequestBody UpdateUserStatusDto updateUserStatusDto){
        int i=userMapper.updateUserStatus(updateUserStatusDto);
        if(updateUserStatusDto.getStatus()==1||updateUserStatusDto.getStatus()==2&&(i!=0)) {
           return CommonResult.success();
        }else{
            return CommonResult.fail(ResultEnum.UPDATE_USERSTATUS_FAIL);
        }
    }

   @PostMapping("/getAllUser")
    public CommonResult getAllUser(){
        List<User> list=userMapper.getAllUser();
            return CommonResult.success(list);
   }

   @PostMapping("/updateUserAvatarUrl")
    public CommonResult updateUserAvatarUrl(@RequestBody UpdateUserAvatarUrlDto updateUserAvatarUrlDto){
        int i=userMapper.updateUserAvatarUrl(updateUserAvatarUrlDto);
        if(i!=0){
            return CommonResult.success();
        }else{
            return CommonResult.fail(ResultEnum.UPDATE_MESSAGE_FAIL);
        }
   }

   @PostMapping("/updateUserTelephone")
    public CommonResult updateUserTelephone(@RequestBody UpdateUserTelephone updateUserTelephone){
        int i=userMapper.updateUserTelephone(updateUserTelephone);
        if(i!=0){
            return CommonResult.success();
        }else{
            return CommonResult.fail(ResultEnum.UPDATE_MESSAGE_FAIL);
        }
   }
}
